<?php


namespace app\index\controller;


use think\facade\Config;
use think\Request;

class Book extends \Common{

    private $token = [];

    # 检验token
    public function __construct(Request $request = null){
        parent::__construct();
        $arr = getallheaders();
        $token = $this->CheckToken(isset($arr["Authorization"]) ? $arr["Authorization"] : "");
        $this->token = $token;
    }

    # 获取书籍列表
    public function GetBooks(){
        if(isset($_GET["page"]) && isset($_GET["limit"])){
            $page = $_GET["page"]; $limit = $_GET["limit"]; $total = 0;
            if(isset($_GET["search_key"]) && isset($_GET["search_value"]) && $_GET["search_value"] != ""){
                $key = $_GET["search_key"]; $value = $_GET["search_value"];
                switch ($key){
                    case "name" : $book = db("book_name")->where("name like '%{$value}%'")->where("is_del", 0)->page($page, $limit)->select(); $total = db("book_name")->where("name like '%{$value}%'")->where("is_del", 0)->count(); break;
                    case "number" : $book = db("book_name")->where("number", $value)->where("is_del", 0)->page($page, $limit)->select(); $total = db("book_name")->where("number", $value)->where("is_del", 0)->count(); break;
                    case "stock_number" : $book = db("book_name")->where("stock_number", $value)->where("is_del", 0)->page($page, $limit)->select(); $book = db("book_name")->where("stock_number", $value)->where("is_del", 0)->count(); break;
                    case "author" : $book = db("book_name")->where("author", $value)->where("is_del", 0)->page($page, $limit)->select(); $total = db("book_name")->where("author", $value)->where("is_del", 0)->count(); break;
                    default : $book = db("book_name")->where("is_del", 0)->page($page, $limit)->select(); $total = db("book_name")->where("is_del", 0)->count(); break;
                }
            }else{
                $book = db("book_name")->where("is_del", 0)->page($page, $limit)->select();
                $total = db("book_name")->where("is_del", 0)->count();
            }
            $books = [];
            if(!empty($book)){
                foreach ($book as $value){
                    $PageCount = db("book_page")->where("bn_id", $value["bn_id"])->where("is_del", 0)->count();
                    $grade = db("grade")->where("gid", $value["gid"])->where("is_del", 0)->find();
                    $class = db("class")->where("cid", $value["cid"])->where("is_del", 0)->find();
                    $language = db("language")->where("lid", $value["lid"])->where("is_del", 0)->find();
                    $books[] = [
                        "id" => (int)$value["bn_id"],
                        "gid" => $value["gid"] == 0 ? "" : (int)$value["gid"],
                        "grade" => empty($grade) ? "" : $grade["name"],
                        "cid" => $value["cid"] == 0 ? "" : (int)$value["cid"],
                        "class" => empty($class) ? "" : $class["name"],
                        "lid" => $value["lid"] == 0 ? "" : (int)$value["lid"],
                        "language" => empty($language) ? "" : $language["name"],
                        "name" => $value["name"],
                        "cover" => $value["cover"] == "" ? "" : Config::get("app.ip")."static/upload/book/cover/{$value["cover"]}?t=".time(),
                        "allpage" => $PageCount,
                        "number" => $value["number"],
                        "stock_number" => $value["stock_number"],
                        "author" => $value["author"],
                        "size" => $value["size"],
                        "remark" => $value["remark"],
                        "is_book" => $value["is_book"] == 1,
                    ];
                }
            }

            $data = [
                "list" => $books,
                "total" => $total
            ];
            Code(200, false, $data);
        }else{
            Code(403, "服务器错误,请稍后再试");
        }
    }

    # 添加编辑书籍
    public function UpdateBook(){
        if(isset($_POST["name"])){
            $info = [
                "gid" => $_POST["gid"] == "" ? 0 : $_POST["gid"],
                "cid" => $_POST["cid"] == "" ? 0 : $_POST["cid"],
                "lid" => $_POST["lid"] == "" ? 0 : $_POST["lid"],
                "name" => $_POST["name"],
                "number" => $_POST["number"],
                "stock_number" => $_POST["stock_number"],
                "author" => $_POST["author"],
                "size" => $_POST["size"],
                "remark" => $_POST["remark"],
                "is_book" => $_POST["is_book"] == "true" ? 1 : 0,
            ];
            if(isset($_POST["id"])){ # 编辑
                $check = db("book_name")->where("is_del", 0)->where("bn_id != {$_POST["id"]}")->where("name", $_POST["name"])->find();
                if(empty($check)){
                    if(isset($_FILES["cover"])){
                        $arr = explode(".", $_FILES["cover"]["name"]);
                        $suffix = end($arr); $name = "{$_POST["id"]}.$suffix";
                        move_uploaded_file($_FILES["cover"]["tmp_name"], "./static/upload/book/cover/$name");
                        $info["cover"] = $name;
                    }
                    db("book_name")->where("bn_id", $_POST["id"])->update($info);
                    Code(200, "修改成功", (Object)[]);
                }else{
                    Code(403, "书籍名称已存在, 请修改");
                }
            }else{ # 新增
                $check = db("book_name")->where("is_del", 0)->where("name", $_POST["name"])->find();
                if(empty($check)){
                    $id = db("book_name")->insertGetId($info);
                    if(isset($_FILES["cover"])){
                        $arr = explode(".", $_FILES["cover"]["name"]);
                        $suffix = end($arr); $name = "$id.$suffix";
                        move_uploaded_file($_FILES["cover"]["tmp_name"], "./static/upload/book/cover/$name");
                        $update = ["cover" => $name];
                        db("book_name")->where("bn_id", $id)->update($update);
                    }
                    Code(200, "添加成功", (Object)[]);
                }else{
                    Code(403, "书籍名称已存在, 请修改");
                }
            }
        }else{
            Code(403, "服务器错误,请稍后再试");
        }
    }

    # 删除书籍
    public function DelBook(){
        if(isset($_POST["id"])){
            $upInfo = [
                "is_del" => 1
            ];
            db("book_name")->where("bn_id", $_POST["id"])->update($upInfo);
            Code(200, "删除成功", "");
        }else{
            Code(403, "服务器错误,请稍后再试");
        }
    }

    # 获取内容
    public function GetPages(){
        if(isset($_GET["book_id"]) && isset($_GET["page"]) && isset($_GET["limit"])){
            $bn_id = $_GET["book_id"];
            $book = db("book_name")->where("bn_id", $bn_id)->find();
            if(isset($_GET["search_value"]) && $_GET["search_value"] != ""){
                $page = db("book_page")->where("bn_id", $bn_id)->where("keyword", $_GET["search_value"])->where("is_del", 0)->page($_GET["page"], $_GET["limit"])->select();
                $total = db("book_page")->where("bn_id", $bn_id)->where("keyword", $_GET["search_value"])->where("is_del", 0)->count();
            }else{
                $page = db("book_page")->where("bn_id", $bn_id)->where("is_del", 0)->page($_GET["page"], $_GET["limit"])->select();
                $total = db("book_page")->where("bn_id", $bn_id)->where("is_del", 0)->count();
            }
            $list = [];
            if(!empty($page)){
                foreach ($page as $value){
                    $list[] = [
                        "id" => (int)$value["bp_id"],
                        "book_name" => $book["name"],
                        "image" => Config::get("app.ip")."static/upload/book/content/{$value["bn_id"]}/{$value["path"]}?t=".time(),
                        "page" => (int)$value["page"],
                        "page_format" => "P".$value["page"],
                        "keyword" => $value["keyword"] == null ? "" : $value["keyword"],
                        "text" => $value["text"] == null ? "" : $value["text"],
                        "text_tran" => $value["text_tran"] == null ? "" : $value["text_tran"],
                        "edit_remark" => $value["edit_remark"] == null ? "" : $value["edit_remark"],
                        "add_remark" => $value["add_remark"] == null ? "" : $value["add_remark"],
                        "reader_remark" => $value["reader_remark"] == null ? "" : $value["reader_remark"],
                    ];
                }
            }
            $data = [
                "list" => $list,
                "total" => $total,
                "book_name" => $book["name"],
            ];
            Code(200, false, $data);
        }else{
            Code(403, "服务器错误,请稍后再试");
        }
    }

    # 删除内页
    public function DelPage(){
        if(isset($_POST["id"]) && isset($_POST["book_id"])){
            $id = $_POST["id"]; $pid = $_POST["book_id"]; $upDel = [ "is_del" => 1 ];
            $check = db("book_page")->where("bn_id", $id)->where("bp_id", $pid)->find();
            if(!empty($check)){
                db("book_page")->where("bn_id", $id)->where("bp_id", $pid)->update($upDel);
                $this->ReOrderBook($pid, $id);
            }
            Code(200, "页面删除成功", "");
        }else{
            Code(403, "服务器错误,请稍后再试");
        }
    }

    # 重新排序书本内页
    private function ReOrderBook($book_id, $id){
        $books = db("book_page")->where("bn_id", $book_id)->where("bp_id > $id")->where("is_del", 0)->select();
        if(!empty($books)){
            foreach ($books as $value){
                $upInfo = [
                    "page" => (int)$value["page"] - 1
                ];
                db("book_page")->where("bn_id", $book_id)->where("bp_id", $value["bp_id"])->update($upInfo);
            }
        }
    }

}